package com.android.RssReader.Adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.android.RssReader.R;
import com.android.RssReader.Provider.NewsDatabaseHelper;
import com.android.RssReader.Screen.ScreenArgs;
import com.android.RssReader.Utils.SecBase64;

public class NewsItemAdapter extends CursorAdapter{
	private LayoutInflater inflater;
	
	
	public NewsItemAdapter(Context context, Cursor c) {
		super(context, c);
		inflater = LayoutInflater.from(context);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		NewsItem news = (NewsItem) view.getTag(R.layout.screen_home_news_item);
		String title = cursor.getString(cursor.getColumnIndex(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_TITLE));
		String description = cursor.getString(cursor.getColumnIndex(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_DESCRIPTION));
		String source = cursor.getString(cursor.getColumnIndex(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_SOURCE));
		String date = cursor.getString(cursor.getColumnIndex(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_DATE));
		String link = cursor.getString(cursor.getColumnIndex(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_LINK));
		String category = cursor.getString(cursor.getColumnIndex(NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_CATEGORY));
		news.title.setText(SecBase64.decode(new String(title)));
		news.description.setText(description);
		news.source.setText(source);
		news.date.setText(date);
		news.link = link;
		ScreenArgs args = new ScreenArgs();
		args.putExtra("title", title);
		args.putExtra("description", description);
		args.putExtra("source", source);
		args.putExtra("date", date);
		args.putExtra("link", link);
		args.putExtra("category", category);
		view.setTag(args);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = inflater.inflate(R.layout.screen_home_news_item, null);
		NewsItem news = new NewsItem();
		news.title=(TextView) view.findViewById(R.id.news_item_title_tv);
		news.description = (TextView) view.findViewById(R.id.news_item_description_tv);
		news.source=(TextView) view.findViewById(R.id.news_item_source_tv);
		news.date = (TextView) view.findViewById(R.id.news_item_time_tv);
		view.setTag(R.layout.screen_home_news_item,news);
		return view;
	}

	private class NewsItem {
		private TextView title;
		private TextView description;
		private TextView source;
		private TextView date;
		private String link;
	}
	
}
